using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ModWatch
{

	/// <summary>
	/// A <see cref="ModWatch.ModWatcherExtenderBase"/> control
	/// that tracks changes to a <see cref="System.Web.UI.WebControls.RadioButton"/>.
	/// </summary>
	[TargetControlType(typeof(RadioButton))]
	public class RadioButtonModWatcherExtender : ModWatcherExtenderBase
	{

		/// <summary>
		/// The <see cref="P:System.Web.UI.WebControls.RadioButton.Checked">Checked</see> value
		/// of the targeted <see cref="System.Web.UI.WebControls.RadioButton"/> at the
		/// time that it was last saved.
		/// </summary>
		private bool SavedValue
		{
			get { return GetProperty("SavedValue", false); }
			set { SetProperty("SavedValue", value); }
		}

		/// <summary>
		/// Raises the <see cref="System.Web.UI.Control.Init"/> event.
		/// </summary>
		/// <param name="e">
		/// An <see cref="System.EventArgs"/>
		/// object that contains event data.
		/// </param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			this.GettingScriptReferences += new EventHandler<ScriptReferencesEventArgs>(this_GettingScriptReferences);
		}

		/// <summary>
		/// Returns a value indicating whether the value of the targeted
		/// <see cref="System.Web.UI.WebControls.RadioButton"/> has changed since it was last saved.
		/// </summary>
		/// <returns>true if a change has been detected, false otherwise.</returns>
		protected override bool HasBeenChanged()
		{
			RadioButton rdo = TargetControl as RadioButton;
			return (SavedValue != rdo.Checked);
		}

		/// <summary>
		/// Saves the current value of the targeted
		/// <see cref="System.Web.UI.WebControls.RadioButton"/>.
		/// </summary>
		public override void SaveTargetState()
		{
			RadioButton rdo = TargetControl as RadioButton;
			SavedValue = rdo.Checked;
		}

		/// <summary>
		/// Returns the <see cref="System.Web.UI.ScriptDescriptor"/>s required by
		/// this instance of <see cref="ModWatch.RadioButtonModWatcherExtender"/>.
		/// </summary>
		/// <param name="targetControl">
		/// The <see cref="System.Web.UI.WebControls.RadioButton"/> control being targeted.
		/// </param>
		/// <returns>
		/// A collection of <see cref="System.Web.UI.ScriptDescriptor"/>s
		/// defining this instance of <see cref="ModWatch.RadioButtonModWatcherExtender"/>.
		/// </returns>
		protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors(Control targetControl)
		{
			ScriptBehaviorDescriptor sbd = new ScriptBehaviorDescriptor("ModWatch.RadioButtonModWatcherBehavior", targetControl.ClientID);
			sbd.AddProperty("modGroup", this.ModGroup);
			sbd.AddProperty("valueToCompare", this.SavedValue);

			return new ScriptDescriptor[] { sbd };
		}

		/// <summary>
		/// Registers the scripts needed by the
		/// <see cref="ModWatch.RadioButtonModWatcherExtender"/> class.
		/// </summary>
		/// <param name="sender">
		/// The <see cref="System.Object"/> that fired the
		/// <see cref="ModWatch.ModWatchExtenderBase.GettingScriptReferences"/> event.
		/// </param>
		/// <param name="e">
		/// A <see cref="ModWatch.ScriptReferencesEventArgs"/> object that contains the event data.
		/// </param>
		private void this_GettingScriptReferences(object sender, ScriptReferencesEventArgs e)
		{
			e.AddWebResourceScriptReference("ModWatch.Scripts.RadioButtonModWatcherBehavior.js");
		}

	}

}
